<template>
  <div>
    <div style="margin-bottom: 5px">
      <el-input placeholder="请输入物品名" style="width: 200px;" suffix-icon="el-icon-search" v-model="name" @keyup.enter.native="loadPost"></el-input>
      <el-select v-model="storage" placeholder="请选择仓库" style="margin-left: 5px">
        <el-option
            v-for="item in storageData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="goodstype" placeholder="请选择物品类名" style="margin-left: 5px">
        <el-option
            v-for="item in goodstypeData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
        </el-option>
      </el-select>
      <el-button type="primary" style="margin-left: 5px;" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>



    </div>
    <el-table :data="tableData"
              :header-cell-style="{ background: '#ced9e3',color: '#555555'}"
              border


    >

      <el-table-column prop="id" label="ID" width="140">
      </el-table-column>
      <el-table-column prop="goodsname" label="物品名" width="180">
      </el-table-column>
      <el-table-column prop="storagename" label="仓库名" width="180">
      </el-table-column>
      <el-table-column prop="goodstypename" label="物品类名" width="180">
      </el-table-column>
      <el-table-column prop="username" label="申请人" width="180">
      </el-table-column>
      <el-table-column prop="adminname" label="操作人" width="180">
      </el-table-column>
      <el-table-column prop="createtime" label="操作时间" width="180">
      </el-table-column>
      <el-table-column prop="count" label="数量" width="180">
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>






    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

  </div>
</template>

<script>
export default {
  name: "RecordManage",
  data() {
    let checkCount = (rule, value, callback) => {
      if(value>9999){
        callback(new Error('数量输⼊过⼤'));
      }else{
        callback();
      }
    };


    return {
      user : JSON.parse(sessionStorage.getItem('CurUser',)),
      goodstypeData:[],
      storageData:[],
      tableData: [],
      pageSize:5,
      pageNum:1,
      total:0,
      name:'',
      storage: '',
      goodstype: '',
      centerDialogVisible:false,
      form:{
        id:'',
        name:'',
        storage:'',
        goodstype:'',
        count:'',
        remark:'',


      },

    }
  },
  methods:{
    formatStorage(row){
      let temp = this.storageData.find(item=>{
        return item.id == row.storage
      })
      return temp && temp.name

    },
    formatGoodstype(row){
      let temp = this.goodstypeData.find(item=>{
        return item.id == row.goodstype
      })
      return temp && temp.name

    },
//     return a && b 如果a是true的话，返回b，否则返回a
//
// return a || b 如果a是true的话，返回a，否则返回b
handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    loadStorage(){
      this.$axios.get('http://localhost:8090/storage/list').then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.storageData=res.data
        }
        else{
          alert('获取数据失败')
        }
      })
    },
    loadGoodstype(){
      this.$axios.get('http://localhost:8090/goodstype/list').then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.goodstypeData=res.data
        }
        else{
          alert('获取数据失败')
        }
      })
    },

    loadPost(){
      this.$axios.post('http://localhost:8090/record/listPageCCC',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,
          goodstype: this.goodstype+'',
          storage: this.storage+'',
          roleId:this.user.roleId+'',
          userId:this.user.id+'',
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.tableData=res.data
          this.total=res.total
        }
        else{
          alert('获取数据失败')
        }
      })
    },

    resetParam(){
      this.name=''
      this.storage=''
      this.goodstype=''
    },
    add(){
      this.centerDialogVisible = true;
      this.$nextTick(()=>{
        this.resetForm()
      })/*清空表单*/


    },



    resetForm(formName) {
      this.$refs.form.resetFields();
    },




  },
  beforeMount() {

    this.loadStorage();
    this.loadGoodstype();

    this.loadPost();

  }
}
</script>

<style scoped>

</style>
